🐛 Fixed flaky unit tests caused by DNS stubs lost after sinon.restore()#27168
🐛 Fixed flaky unit tests caused by DNS stubs lost after sinon.restore()#27168
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughTests stopped stubbing DNS globally in several unit tests: request-external, ghost-mailer, and mention-discovery-service no longer import or stub Node DNS. The mention-sending-service now stubs DNS resolution locally inside a single test to return 127.0.0.1. The global test overrides (ghost/core/test/utils/overrides.js) update mochaHooks.afterEach to always call mockManager.disableNetwork() in a try/finally, ensuring network/DNS blocking is re-applied after per-test cleanup. No public APIs or exported declarations were changed. 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@ghost/core/test/utils/overrides.js`:
- Around line 106-115: The afterEach hook currently calls originalAfterEach()
and then mockManager.disableNetwork(), but if originalAfterEach() throws the
disableNetwork call is skipped; update the logic in the afterEach override
(where originalAfterEach is invoked) to call originalAfterEach() inside a
try/finally and move mockManager.disableNetwork() into the finally block so
disableNetwork always runs even when originalAfterEach throws.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d2876cbd-cfed-4d8a-a0eb-1411bdb282e4
📒 Files selected for processing (5)
ghost/core/test/unit/server/lib/request-external.test.jsghost/core/test/unit/server/services/mail/ghost-mailer.test.jsghost/core/test/unit/server/services/mentions/mention-discovery-service.test.jsghost/core/test/unit/server/services/mentions/mention-sending-service.test.jsghost/core/test/utils/overrides.js
💤 Files with no reviewable changes (2)
- ghost/core/test/unit/server/lib/request-external.test.js
- ghost/core/test/unit/server/services/mail/ghost-mailer.test.js
ref https://github.com/TryGhost/Ghost/actions/runs/24012086446/job/70025449313 The global disableNetwork() helper stubs dnsPromises.lookup via sinon during beforeAll to prevent real DNS lookups in tests. However, many tests call sinon.restore() in their afterEach, which nukes all sinon stubs — including the DNS one. Subsequent tests then perform real DNS lookups against nocked domains (e.g. external.com), which can be slow or unreliable on CI, causing intermittent 2000ms timeouts. This was the root cause of the flaky ExternalMediaInliner CDN storage adapter test failures. The fix re-applies disableNetwork() in the global mocha root afterEach hook, which runs after every test's own afterEach. This ensures DNS stubs are always restored after sinon.restore() removes them. Three tests that were redundantly re-stubbing DNS with the same fake values are cleaned up, and one test that was silently depending on the stub being absent (to get a real private-IP DNS result) is updated to explicitly stub the behavior it needs. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
7574deb to
113c166
Compare
Wrapped the originalAfterEach() call in try/finally so disableNetwork() still re-applies DNS stubs even if a previous afterEach hook throws. Without this, a single failing hook would silently reintroduce cross-test DNS flakiness. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|



Summary
disableNetwork()helper stubsdnsPromises.lookupvia sinon duringbeforeAllto prevent real DNS lookups in testssinon.restore()in theirafterEach, which removes all sinon stubs — including the DNS oneexternal.com), which can be slow or unreliable on CI, causing intermittent 2000ms timeoutsExternalMediaInlinerCDN storage adapter test failures seen in https://github.com/TryGhost/Ghost/actions/runs/24012086446/job/70025449313What changed
test/utils/overrides.js— Re-applydisableNetwork()in the global mocha rootafterEachhook, so DNS stubs are always present for the next testdisableNetwork()already provides (request-external.test.js,ghost-mailer.test.js,mention-discovery-service.test.js)mention-sending-service.test.js— Fixed a test that was silently depending on the DNS stub being absent (so real DNS would resolvedomaincontrol.com→127.0.0.1). Now explicitly stubs DNS to return a private IP, making it deterministicTest plan
external-media-inlinertests passrequest-external,ghost-mailer,mention-discovery-service,mention-sending-servicetests passyarn test:unitsuite passes (only pre-existing Node version mismatch failure)🤖 Generated with Claude Code